<?php
namespace app\api\controller;

class InsurancePlan extends Permission
{
    /*
        这个是旧的了 2024年09月06日
        status 状态备注

        0 待报价

        10 待选择(待查看)

        20 待出单（叫保险公司买）

        30 已出单待确认(叫保险公司买，但还没得到确切答复，或者没填写完整价格返点)

        80 不要的报价

        90 已完成（买完，拿完保单回来）

        choose_status 报价查看状态
        1 待查看
        2 已沟通待客户回复
        3 重新报价
    */
    /*
        # 2024年09月06日 最新备注
        0 待报价
        10 待查看
        20 已阅
    */
    public function getList()
    {
        $page = isset($this->input_data['page']) ? $this->input_data['page'] : 1;
        $limit = isset($this->input_data['limit']) ? $this->input_data['limit'] : 15;
        $map = [];

        if( isset($this->input_data['cate']) && $this->input_data['cate'] != '' ) $map['cate'] = $this->input_data['cate'];

        if( isset($this->input_data['status']) && $this->input_data['status'] != 99999999 ) $map['a.status'] = $this->input_data['status'];

        if( isset($this->input_data['type']) && $this->input_data['type'] != '所有类型' ) $map['type'] = $this->input_data['type'];

        if(isset($this->input_data['company']) && $this->input_data['company'] != '') $map['company'] = ['LIKE','%'.$this->input_data['company'].'%'];

        // if(isset($this->input_data['area_id']) && $this->input_data['area_id'] != 0) $map['area_id'] = $this->input_data['area_id'];
        if(isset($this->input_data['vehicle_id']) && $this->input_data['vehicle_id'] != 0) $map['vehicle_id'] = $this->input_data['vehicle_id'];

        if(isset($this->input_data['plate_number']) && $this->input_data['plate_number'] !== '') $map['a.plate_number'] = ['LIKE','%'.$this->input_data['plate_number'].'%'];
        
        if($this->post('area_id') > 0)
        {
            $map['area_id'] = $this->post('area_id');
        }
        else
        {
            $map['area_id'] = ['IN',$this->myAllAreaIds];
        }

        $total = db('InsurancePlan')->alias('a')->join('vehicle b','a.vehicle_id = b.id','LEFT')->where($map)->count();
        $rows = db('InsurancePlan')->alias('a')->join('vehicle b','a.vehicle_id = b.id','LEFT')->field('a.*')->order('id', 'desc')->page($page,$limit)->where($map)->select();

        $reoffer = $apply = $choose = $wating = $cantbuy = 0;
        
        $rMap = $map;
        $rMap['cate'] = '重新报价';
        $reoffer = db('InsurancePlan')->alias('a')->join('vehicle b','a.vehicle_id = b.id','LEFT')->where($rMap)->count();
        $rMap['cate'] = '人工申请';
        $apply = db('InsurancePlan')->alias('a')->join('vehicle b','a.vehicle_id = b.id','LEFT')->where($rMap)->count();
        
        $badgeData = [
            'apply' => $apply,
            'reoffer' => $reoffer
        ];


        $list = [];
        foreach ($rows as $v) {
            $iMap = [
                'vehicle_id' => $v['vehicle_id'],
                'status' => 0
            ];
            $v['offer_times'] = db('Insurance')->where($iMap)->count();
            $v['create_time'] = date('Y-m-d H:i',$v['create_time']);
            $v['offer_time'] = date('Y-m-d H:i',$v['offer_time']);
            $list[] = $v;
        }
        
        $data = [
            'total' => $total,
            'list' => $list,
            'badgeData' => $badgeData,
            'map' => $map
        ];
        $this->resSuc($data);
    }

    public function updatePdf()
    {
        $map = [
            'id' => $this->input_data['id']
        ];

        $data = [];

        $this->input_data['type'] == '交强险' ? $data['pdf_c'] = $this->input_data['url'] : $data['pdf_b'] = $this->input_data['url'];
        db('InsurancePlan')->where($map)->update($data);

        $this->resSuc('ok');
    }

    // 原来是提交到选择，现在是报价完成
    public function toChoose()
    {
        $map = [
            'id' => $this->input_data['id']
        ];
        $data = [
            'status' => 10,
            'offer_user' => $this->user['realname'],
            'offer_user_id' => $this->user['id'],
            'offer_time' => time(),
        ];

        db('InsurancePlan')->where($map)->update($data);

        // $this->_updateTradeOfferTimes($this->input_data['trade_id']);

        $this->resSuc('ok');
    }

    private function _updateTradeOfferTimes($tradeId)
    {
        $map = [
            'trade_id' => $tradeId
        ];
        $rows = db('Insurance')->where($map)->select();
        $cp = $hy = $jy = $az = $cy = $c = $b  = 0;
        foreach ($rows as $v) {
            switch ($v['cate']) {
                case '交强险':
                    $c++;
                    break;
                case '商业险':
                    $b++;
                    break;
                case '超赔险':
                    $cp++;
                    break;
                case '货运险':
                    $hy++;
                    break;
                case '驾意险':
                    $jy++;
                    break;
                case '安责险':
                    $az++;
                    break;
                case '承运险':
                    $cy++;
                    break;
                
                default:
                    // code...
                    break;
            }
        }

        $pMap = [
            'id' => $tradeId
        ];
        $data = [
            'qx_offer' => $c,
            'syx_offer' => $b,
            'cpx_offer' => $cp,
            'cyx_offer' => $cy,
            'azx_offer' => $az,
            'hyx_offer' => $hy,
            'jyx_offer' => $jy,
        ];
        db('Trade')->where($pMap)->update($data);
    }

    // 创建报价计划
    public function createPlan()
    {
        if($this->input_data['action'] == 'reoffer')
        {
            $this->_reCreate();
        }
        else
        {
            $this->_create();
        }
    }

    private function _reCreate()
    {
        // 旧的报价设为重新报价
        $map = [
            'id' => $this->input_data['id']
        ];
        $data = [
            'choose_status' => 3
        ];
        db('InsurancePlan')->where($map)->update($data);
        
        // 生成新的报价申请
        $old = db('InsurancePlan')->where($map)->find();
        $data = [];
        $data['year'] = $old['year'];
        $data['create_time'] = time();
        $data['create_user'] = $this->user['realname'];
        $data['create_user_id'] = $this->user['id'];
        $data['remark'] = $this->input_data['remark'];
        $data['cate'] = '重新报价';
        $data['vehicle_id'] = $old['vehicle_id'];
        $data['plate_number'] = $old['plate_number'];
        $data['company'] = $old['company'];
        $data['deadline_c'] = $old['deadline_c'];
        $data['deadline_b'] = $old['deadline_b'];

        db('InsurancePlan')->insert($data);

        $this->resSuc('ok');
    }

    private function _create()
    {
        $map = [
            'id' => $this->input_data['id']
        ];
        $data = [
            'cate' => '人工申请',
            // 'remark_c' => $this->input_data['insurance_c_remark'],
            // 'remark_b' => $this->input_data['insurance_b_remark'],
            'remark' => $this->input_data['remark'],
            'vehicle_id' => $this->input_data['id'],
            'company' => $this->input_data['company'],
            'plate_number' => $this->input_data['plate_number'],
            'create_user' => $this->user['realname'],
            'create_user_id' => $this->user['id'],
            'create_time' => time(),
        ];

        db('InsurancePlan')->insert($data);

        $this->resSuc('ok');
    }
    // 等待车主回复操作
    public function watingReply()
    {
        $map = [
            'id' => $this->input_data['id']
        ];
        $data = [
            'wating_content' => $this->input_data['wating_content'],
            'wating_time' => time(),
            'wating_user' => $this->user['realname'],
            'wating_user_id' => $this->user['id'],
            'choose_status' => 2
        ];
        db('InsurancePlan')->where($map)->update($data);

        $this->resSuc('ok');
    }

    // 无法出单操作
    public function cantBuy()
    {
        // 调整报价单状态
        $map = [
            'id' => $this->input_data['id']
        ];
        $data = [
            'status' => 10,
            'cantbuy_content' => $this->input_data['cantbuy_content'],
            'cantbuy_time' => time(),
            'cantbuy_user' => $this->user['realname'],
            'cantbuy_user_id' => $this->user['id'],
            'choose_status' => 4
        ];
        db('InsurancePlan')->where($map)->update($data);

        // 删除报价单对应的其他险种(超赔，其他险那些)
        $map = [
            'plan_id' => $this->input_data['id'],
            'cate' => ['NOT IN',['交强险','商业险']]
        ];
        db('Insurance')->where($map)->delete();
        $this->resSuc('ok');
    }

    // 设置报价单状态
    public function setStatus()
    {
        db('InsurancePlan')->where(['id'=>$this->input_data['id']])->update(['status'=>$this->input_data['status']]);
        $this->resSuc('ok');
    }

    // 重新报价
    public function reOffer()
    {
        // 旧的报价设为已阅
        $map = [
            'id' => $this->input_data['id']
        ];
        $data = [
            'status' => 20
        ];
        db('InsurancePlan')->where($map)->update($data);
        
        // 生成新的报价申请
        $old = db('InsurancePlan')->where($map)->find();
        $data = [];
        $data['year'] = $old['year'];
        $data['create_time'] = time();
        $data['create_user'] = $this->user['realname'];
        $data['create_user_id'] = $this->user['id'];
        $data['remark'] = $this->input_data['remark'];
        $data['cate'] = '重新报价';
        $data['vehicle_id'] = $old['vehicle_id'];
        $data['plate_number'] = $old['plate_number'];
        $data['company'] = $old['company'];
        $data['trade_id'] = $old['trade_id'];
        $data['cp'] = $old['cp'];
        $data['az'] = $old['az'];
        $data['cy'] = $old['cy'];
        $data['jy'] = $old['jy'];
        $data['hy'] = $old['hy'];

        db('InsurancePlan')->insert($data);

        $this->resSuc('ok');
    }
}
